package com.kingint.web.portal.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kingint.mbg.bas.entity.BasOrderTypeHotelTime;
import com.kingint.mbg.bas.entity.BasWorkOrderType;
import com.kingint.mbg.bas.mapper.BasOrderTypeHotelTimeMapper;
import com.kingint.mbg.bas.mapper.BasWorkOrderTypeMapper;
import com.kingint.mbg.ser.entity.SerReceiveWorkMiddle;
import com.kingint.mbg.ser.entity.SerWorkOrder;
import com.kingint.mbg.ser.mapper.SerReceiveWorkMiddleMapper;
import com.kingint.mbg.ser.mapper.SerWorkOrderMapper;
import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.web.admin.base.service.BasWorkOrderTypeService;
import com.kingint.web.admin.serve.service.SerReceiveWorkMiddleService;
import com.kingint.web.portal.base.service.WorkOrderTypeService;
import com.kingint.web.sys.service.RedisService;
import com.kingint.web.sys.service.UmsAdminService;
import com.kingint.web.util.DateUtil;
import com.kingint.web.util.WorkType;
import com.kingint.web.util.spring.SpringContextHolder;
import com.sun.org.apache.bcel.internal.generic.NEW;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 工单类型表 服务实现类
 * </p>
 *
 * @author wxh
 * @since 2020-12-21
 */
@Service
@Slf4j
public class WorkOrderTypeServiceImpl extends ServiceImpl<BasWorkOrderTypeMapper, BasWorkOrderType> implements WorkOrderTypeService {

    @Resource
    private BasWorkOrderTypeMapper workOrderTypeMapper;
    @Resource
    private BasOrderTypeHotelTimeMapper orderTypeHotelTimeMapper;
    @Resource
    private UmsAdminService adminService;
    @Resource
    private SerWorkOrderMapper workOrderMapper;
    @Resource
    private SerReceiveWorkMiddleService receiveWorkMiddleService;
    @Resource
    private RedisService redisService;

    @Override
    public BasWorkOrderType getWorkOrderUploadTime(String name) {
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
        if (StringUtils.isEmpty(hotelId)) {
            log.debug("当前账号没有关联酒店id");
            return null;
        }
        QueryWrapper<BasWorkOrderType> wrapper = new QueryWrapper<>();
        wrapper.like("name", name);
        BasWorkOrderType type = workOrderTypeMapper.selectOne(wrapper);
        if (!StringUtils.isEmpty(type)) {
            Integer id = type.getId();//工单类型id
            //先查询有没有自定上报时间
            QueryWrapper<BasOrderTypeHotelTime> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("hotel_id", hotelId)
                    .eq("word_type_id", id);
            BasOrderTypeHotelTime info = orderTypeHotelTimeMapper.selectOne(queryWrapper);
            if (StringUtils.isEmpty(info)) {
                return type;
            }
            BasWorkOrderType orderType = new BasWorkOrderType();
            BeanUtils.copyProperties(info, orderType);
            return orderType;
        }
        return null;
    }

    @Override
    public int judgeWorkOrderIsTimeOut(Long userId,Integer workId, String name) {
        BasWorkOrderType info = getWorkOrderUploadTime(name);
        if (!StringUtils.isEmpty(info)) {
            Integer time = info.getReportDirectorTime() == null ? 0 : info.getReportDirectorTime();//分钟
            //查询工单的开始时间
            SerWorkOrder workOrder = workOrderMapper.selectById(workId);
            Date startTime = workOrder.getStartTime();//开始时间
            long time1 = DateUtil.addTimeMinute(startTime, time).getTime();//对应的时间
            long time2 = new Date().getTime();//当前时间
            if (time2 > time1) {//更新接单状态 超时
                SerReceiveWorkMiddle workMiddle = new SerReceiveWorkMiddle();
                workMiddle.setWorkId(workId);
                workMiddle.setIsTimeOut(1);//超时
                workMiddle.setReceiveUserId(userId);
                int i = receiveWorkMiddleService.updateWorkInfo(workMiddle);
                return i;
            }
        }
        return 0;
    }

    @Override
    public void addRedisHashInfo(SerWorkOrder serWorkOrder) {
        String key1="hotelktneed";
        //酒店ID_workId_workType_时间戳
        Integer hotelId = serWorkOrder.getHotelId();
        Integer id = serWorkOrder.getId();//workId
        Integer workTypeId = serWorkOrder.getWorkTypeId();//对应工单类型id

        String key2=hotelId+"_"+id+"_"+workTypeId;
        String value=serWorkOrder.getStartTime().getTime()+"";
        log.debug("存入redis相关内容："+key1+":"+key2+":"+value);
        log.info("存入redis相关内容："+key1+":"+key2+":"+value);
        redisService.hset(key1,key2,value);//存入redis
    }
}
